package com.jy.api.controller.admin;

import com.jy.api.service.ProductSupplyTimeService;
import com.jy.api.signUpReq.admin.ProductSupplyTimesSignUpReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lihu on 2017/12/27.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/admin/", produces = "application/json;charset=utf-8")
public class ProductSupplyTimeController {

    @Autowired
    ProductSupplyTimeService productSupplyTimeService;

    /**
     * 获取所有店铺时段
     *
     * @param storeId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "product-supply-time")
    public ResponseEntity getProductSupplyTimes(@RequestParam(name = "storeId") String storeId) {
        try {
            return new ResponseEntity(productSupplyTimeService.getStoreAll(storeId), HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getProductTypes is error=>storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加商品时段管理
     *
     * @param signUpReq
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "product-supply-time")
    public ResponseEntity addProductSupplyTimes(@Valid @RequestBody ProductSupplyTimesSignUpReq signUpReq,
                                                BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                return new ResponseEntity(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST);
            }
            if (signUpReq.getBeginHour() > signUpReq.getEndHour()) {
                return new ResponseEntity("时间参数有误！", HttpStatus.BAD_REQUEST);
            } else if (signUpReq.getBeginHour() == signUpReq.getEndHour() &&
                    signUpReq.getBeginMinute() > signUpReq.getEndMinute()) {
                return new ResponseEntity("时间参数有误！", HttpStatus.BAD_REQUEST);
            }

            productSupplyTimeService.saveOrUpdateSupplyTime(signUpReq);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("addProductSupplyTimes is error=>signUpReq:{}", signUpReq, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 删除店铺时段
     *
     * @param id
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "product-supply-time")
    public ResponseEntity deleteProductSupplyTimes(@RequestParam(name = "id") String id) {
        try {

            productSupplyTimeService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            log.error("deleteProductSupplyTimes is error=>id:{}", id, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


}
